#include <stdio.h>
#include <signal.h>
#include <unistd.h>

/**
  * purpose: shows use of sigaction()
  * feature: blocks Ctrl-\ while handling Ctrl-C
  *			 does not reset Ctrl-C handler, so two kill
  */

#define INPUTLEN 100

int main() {
	struct sigaction newhandler;			// new settings
	sigset_t blocked;						// set of blocked sigs
	void inthandler();
	char x[INPUTLEN];

	/* load these two members first */
	newhandler.sa_handler = inthandler;				    // handler function
	newhandler.sa_flags = SA_RESETHAND | SA_RESTART;	// options

	/* then build the list of blocked signals */
	sigemptyset(&blocked);					// clear all bits
	sigaddset(&blocked, SIGQUIT);			// add SIGQUIT to list
	newhandler.sa_mask = blocked;			// stroe blockmask
	
	if (sigaction(SIGINT, &newhandler, NULL) == -1) {
		perror("sigaction");
	} else {
		while(1) {
			fgets(x, INPUTLEN, stdin);
			printf("input: %s", x);
		}
	}
}


void inthandler(int s) {
	printf("Called with signal %d\n", s);
	sleep(s);
	printf("done handling signal %d\n", s);
}
